package com.hdcircles.autoform.util;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hdcircles.autoform.R;
import com.hdcircles.autoform.app.App;

/**
 * Created by hdcircles on 2016/9/21.
 */

public class ToastUtil {
    static ToastUtil td;
    Context context;
    Toast toast;
    String msg;

    public ToastUtil(Context context){
        this.context=context;
    }

    public Toast create(){
        View contentView=View.inflate(context, R.layout.dialog_toast,null);
        TextView tvMsg=(TextView)contentView.findViewById(R.id.tv_toast_msg);
        toast=new Toast(context);
        toast.setView(contentView);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_LONG);
        tvMsg.setText(msg);
        return toast;
    }

    public Toast createShort(){
        View contentView=View.inflate(context,R.layout.dialog_toast,null);
        TextView tvMsg= (TextView) contentView.findViewById(R.id.tv_toast_msg);
        toast=new Toast(context);
        toast.setView(contentView);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_SHORT);
        tvMsg.setText(msg);
        return toast;
    }

    public void show(){
        if (null!=toast){
            toast.show();
        }
    }

    public static void show(int resId){
        show(App.getInstance().getString(resId));
    }

    public static void show(String msg){
        if (null==td){
            td=new ToastUtil(App.getInstance());
        }
        td.setText(msg);
        td.create().show();
    }

    public static void shortShow(String msg){
        if (null==td){
            td=new ToastUtil(App.getInstance());
        }
        td.setText(msg);
        td.createShort().show();
    }


    public void setText(String text){
        msg=text;
    }
}
